package com.punai.dataanalysis.epdatakb.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.punai.common.core.domain.BaseEntity;
import com.punai.common.utils.DateUtils;
import com.punai.common.utils.NumberUtil;
import com.punai.dataanalysis.epdatakb.bo.JtczResultBo;
import com.punai.dataanalysis.epdatakb.domain.DataAnalysisNdgccbqkhzb;
import com.punai.dataanalysis.epdatakb.service.NG001change1212.NG001JyCzBbService;
import com.punai.dataanalysis.epdatakb.vo.DataAnalysisVo;
import com.punai.dataanalysis.ng001.bo.YearLjWccz;
import com.punai.dataanalysis.ng001.domain.ProjectTable;
import com.punai.dataanalysis.ng001.mapper.JyczbbMapper;
import com.punai.dataanalysis.ng001.mapper.ProjectTableMapper;
import com.punai.prodatalibrary.domain.bo.GlobalProInfoBo;
import com.punai.prodatalibrary.service.ILibProjectInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.punai.dataanalysis.epdatakb.mapper.DataAnalysisJyczbbMapper;
import com.punai.dataanalysis.epdatakb.domain.DataAnalysisJyczbb;
import com.punai.dataanalysis.epdatakb.service.IDataAnalysisJyczbbService;

/**
 * 经营产值报Service业务层处理
 *
 * @author zxw_
 * @date 2023-04-20
 */
@Service
public class DataAnalysisJyczbbServiceImpl extends ServiceImpl<DataAnalysisJyczbbMapper,DataAnalysisJyczbb> implements IDataAnalysisJyczbbService
{
    @Autowired
    private DataAnalysisJyczbbMapper dataAnalysisJyczbbMapper;
    @Autowired
    private ProjectTableMapper projectTableMapper;

    @Autowired
    private ILibProjectInfoService libProjectInfoService;

    @Autowired
    private JyczbbMapper jyczbbMapper;

    @Autowired
    private NG001JyCzBbService ng001JyCzBbService;

    @Override
    public List<? extends BaseEntity> getDataList(DataAnalysisVo dataAnalysisVo) {
        if (isC1212) {
            List<? extends BaseEntity> dataList = ng001JyCzBbService.getDataList(dataAnalysisVo);
            return dataList;
        }
        List<GlobalProInfoBo> list = libProjectInfoService.selectInfoListNoPerm(new GlobalProInfoBo());
        List<DataAnalysisJyczbb> res = new ArrayList<>();
        List<String> pmInfoIds = list.stream().map(GlobalProInfoBo::getPmproinfoid).collect(Collectors.toList());
        List<ProjectTable> projectTables = projectTableMapper.selectJyczbbListByVo(pmInfoIds);
        pmInfoIds = projectTables.stream().map(ProjectTable::getPhid).filter(Objects::nonNull).collect(Collectors.toList());
        if (CollUtil.isEmpty(pmInfoIds)) {
            return new ArrayList<>();
        }

        Map<String, ProjectTable> projectTableMap = projectTables.stream().collect(Collectors.toMap(ProjectTable::getPhid, Function.identity(), (v1, v2) -> v2));
        List<JtczResultBo> glrnsl = projectTableMapper.glrnsl(pmInfoIds);
        Map<String, BigDecimal> glrnslMap = glrnsl.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));
        List<YearLjWccz> qnYs = qnYs(pmInfoIds);
        Map<String, YearLjWccz> qnYsMap = qnYs.stream().collect(Collectors.toMap(YearLjWccz::getPc, Function.identity(), (v1, v2) -> v2));
        List<YearLjWccz> qnCb = qnCb(pmInfoIds);
        Map<String, YearLjWccz> qnCbMap = qnCb.stream().collect(Collectors.toMap(YearLjWccz::getPc, Function.identity(), (v1, v2) -> v2));
        List<YearLjWccz> bnCz = bnCz(pmInfoIds);
        Map<String, YearLjWccz> bnCzMap = bnCz.stream().collect(Collectors.toMap(YearLjWccz::getPc, Function.identity(), (v1, v2) -> v2));
        List<YearLjWccz> bnCb = bnCb(pmInfoIds);
        Map<String, YearLjWccz> bnCbMap = bnCb.stream().collect(Collectors.toMap(YearLjWccz::getPc, Function.identity(), (v1, v2) -> v2));
        List<YearLjWccz> nzCz = nzCz(pmInfoIds);
        Map<String, YearLjWccz> nzCzMap = nzCz.stream().collect(Collectors.toMap(YearLjWccz::getPc, Function.identity(), (v1, v2) -> v2));
        List<YearLjWccz> nzCb = nzCb(pmInfoIds);
        Map<String, YearLjWccz> nzCbMap = nzCb.stream().collect(Collectors.toMap(YearLjWccz::getPc, Function.identity(), (v1, v2) -> v2));

        for (GlobalProInfoBo globalProInfoBo : list) {
            DataAnalysisJyczbb jy = new DataAnalysisJyczbb();
            if(StrUtil.isNotEmpty(globalProInfoBo.getPmproinfoid())){
                String pc = globalProInfoBo.getPmproinfoid();
                ProjectTable projectTable = projectTableMap.get(pc);
                if(projectTable == null){
                    continue;
                }

                YearLjWccz qncz = qnYsMap.get(pc);
                YearLjWccz qncb = qnCbMap.get(pc);
                YearLjWccz bncz = bnCzMap.get(pc);
                YearLjWccz bncb = bnCbMap.get(pc);
                YearLjWccz nzcz = nzCzMap.get(pc);
                YearLjWccz nzcb = nzCbMap.get(pc);
                jy = DataAnalysisJyczbb.conversion(jy,projectTable);
                jy.setGlryrs(glrnslMap.getOrDefault(pc,BigDecimal.ZERO));
                jy.setYearYs(qncz.getPdzhj());
                jy.setYearJgc(qncz.getJgc());
                jy.setYearSj(qncz.getSj());
                jy.setYearCost(qncb.getPdzhj());
                jy.setYearCostJgc(qncb.getJgc());

                jy.setYearCzYs(bncz.getPdzhj());
                jy.setYearCzJgc(bncz.getJgc());
                jy.setYearCzSj(bncz.getSj());
                jy.setYearCzTj(bncz.getTj());
                jy.setYearCzAz(bncz.getAz());

                jy.setBenYearCost(bncb.getPdzhj());
                jy.setBenYearJgc(bncb.getJgc());

                jy.setNodeBeforeYs(nzcz.getPdzhj());
                jy.setNodeBeforeYsJgc(nzcz.getJgc());
                jy.setNodeBeforeYsSj(nzcz.getSj());

                jy.setNodeBeforeCb(nzcb.getPdzhj());
                jy.setNodeBeforeCbJgc(nzcz.getJgc());

                calcXjZb(jy);
            }
            res.add(jy);
        }
        DataAnalysisJyczbb zj = zj(res);
        zj.setProName("总合计");
        res.add(zj);

        return res;
    }

    private DataAnalysisJyczbb zj(List<DataAnalysisJyczbb> res) {
        DataAnalysisJyczbb zj = new DataAnalysisJyczbb();
        BigDecimal htTotal = BigDecimal.ZERO;
        BigDecimal jzmj = BigDecimal.ZERO;
        BigDecimal glryrs = BigDecimal.ZERO;
        BigDecimal sgtYs = BigDecimal.ZERO;
        BigDecimal sgtJgc = BigDecimal.ZERO;
        BigDecimal yearYs = BigDecimal.ZERO;
        BigDecimal yearJgc = BigDecimal.ZERO;
        BigDecimal yearSj = BigDecimal.ZERO;
        BigDecimal yearCost = BigDecimal.ZERO;
        BigDecimal yearCostJgc = BigDecimal.ZERO;
        BigDecimal yearLr = BigDecimal.ZERO;
        BigDecimal yearCzYs = BigDecimal.ZERO;
        BigDecimal yearCzJgc = BigDecimal.ZERO;
        BigDecimal yearCzSj = BigDecimal.ZERO;
        BigDecimal yearCzSubTotal = BigDecimal.ZERO;
        BigDecimal yearCzTj = BigDecimal.ZERO;
        BigDecimal yearCzAz = BigDecimal.ZERO;
        BigDecimal yearCzQt = BigDecimal.ZERO;
        BigDecimal yearCzZyfb = BigDecimal.ZERO;
        BigDecimal benYearCost = BigDecimal.ZERO;
        BigDecimal benYearJgc = BigDecimal.ZERO;
        BigDecimal benYearLr = BigDecimal.ZERO;
        BigDecimal nodeBeforeYs = BigDecimal.ZERO;
        BigDecimal nodeBeforeYsJgc = BigDecimal.ZERO;
        BigDecimal nodeBeforeYsSj = BigDecimal.ZERO;
        BigDecimal nodeBeforeCb = BigDecimal.ZERO;
        BigDecimal nodeBeforeCbJgc = BigDecimal.ZERO;
        BigDecimal klLr = BigDecimal.ZERO;
        BigDecimal fzcCostFee = BigDecimal.ZERO;
        for (DataAnalysisJyczbb re : res) {
            htTotal = NumberUtil.add(htTotal,re.getHtTotal());
            jzmj = NumberUtil.add(jzmj,re.getJzmj());
            glryrs = NumberUtil.add(glryrs,re.getGlryrs());
            sgtYs = NumberUtil.add(sgtYs,re.getSgtYs());
            sgtJgc = NumberUtil.add(sgtJgc,re.getSgtJgc());
            yearYs = NumberUtil.add(yearYs,re.getYearYs());
            yearJgc = NumberUtil.add(yearJgc,re.getYearJgc());
            yearSj = NumberUtil.add(yearSj,re.getYearSj());
            yearCost = NumberUtil.add(yearCost,re.getYearCost());
            yearCostJgc = NumberUtil.add(yearCostJgc,re.getYearCostJgc());
            yearLr = NumberUtil.add(yearLr,re.getYearLr());
            yearCzYs = NumberUtil.add(yearCzYs,re.getYearCzYs());
            yearCzJgc = NumberUtil.add(yearCzJgc,re.getYearCzJgc());
            yearCzSj = NumberUtil.add(yearCzSj,re.getYearCzSj());
            yearCzSubTotal = NumberUtil.add(yearCzSubTotal,re.getYearCzSubTotal());
            yearCzTj = NumberUtil.add(yearCzTj,re.getYearCzTj());
            yearCzAz = NumberUtil.add(yearCzAz,re.getYearCzAz());
            yearCzQt = NumberUtil.add(yearCzQt,re.getYearCzQt());
            yearCzZyfb = NumberUtil.add(yearCzZyfb,re.getYearCzZyfb());
            benYearCost = NumberUtil.add(benYearCost,re.getBenYearCost());
            benYearJgc = NumberUtil.add(benYearJgc,re.getBenYearJgc());
            benYearLr = NumberUtil.add(benYearLr,re.getBenYearLr());
            nodeBeforeYs = NumberUtil.add(nodeBeforeYs,re.getNodeBeforeYs());
            nodeBeforeYsJgc = NumberUtil.add(nodeBeforeYsJgc,re.getNodeBeforeYsJgc());
            nodeBeforeYsSj = NumberUtil.add(nodeBeforeYsSj,re.getNodeBeforeYsSj());
            nodeBeforeCb = NumberUtil.add(nodeBeforeCb,re.getNodeBeforeCb());
            nodeBeforeCbJgc = NumberUtil.add(nodeBeforeCbJgc,re.getNodeBeforeCbJgc());
            klLr = NumberUtil.add(klLr,re.getKlLr());
            fzcCostFee = NumberUtil.add(fzcCostFee,re.getFzcCostFee());
        }
        zj.setHtTotal(htTotal);
        zj.setJzmj(jzmj);
        zj.setGlryrs(glryrs);
        zj.setSgtYs(sgtYs);
        zj.setSgtJgc(sgtJgc);
        zj.setYearYs(yearYs);
        zj.setYearJgc(yearJgc);
        zj.setYearSj(yearSj);
        zj.setYearCost(yearCost);
        zj.setYearCostJgc(yearCostJgc);
        zj.setYearLr(yearLr);
        zj.setYearCzYs(yearCzYs);
        zj.setYearCzJgc(yearCzJgc);
        zj.setYearCzSj(yearCzSj);
        zj.setYearCzSubTotal(yearCzSubTotal);
        zj.setYearCzTj(yearCzTj);
        zj.setYearCzAz(yearCzAz);
        zj.setYearCzQt(yearCzQt);
        zj.setYearCzZyfb(yearCzZyfb);
        zj.setBenYearCost(benYearCost);
        zj.setBenYearJgc(benYearJgc);
        zj.setBenYearLr(benYearLr);
        zj.setNodeBeforeYs(nodeBeforeYs);
        zj.setNodeBeforeYsJgc(nodeBeforeYsJgc);
        zj.setNodeBeforeYsSj(nodeBeforeYsSj);
        zj.setNodeBeforeCb(nodeBeforeCb);
        zj.setNodeBeforeCbJgc(nodeBeforeCbJgc);
        zj.setKlLr(klLr);
        zj.setFzcCostFee(fzcCostFee);
        zj.hb();
        calcXjZb(zj);
        return zj;
    }

    private void calcXjZb(DataAnalysisJyczbb jy) {

        // excel上 未标注算法 和之前的表单一样把
        BigDecimal qnYsHj = jy.getYearYs();
        BigDecimal qnYsJgc = jy.getYearJgc();
        BigDecimal qnCbHj = jy.getYearCost();
        BigDecimal qnLr = NumberUtil.subtract(qnYsHj, qnCbHj);
        jy.setYearLr(qnLr);
        jy.setYearLrl(NumberUtil.divideUseProportion(qnLr,2,NumberUtil.add(qnYsHj,qnYsJgc)));

        // 本年
        BigDecimal currYearCzYs =  jy.getYearCzYs();
        BigDecimal currYearCzSj =  jy.getYearCzSj();
        BigDecimal currYearCzJgc = jy.getYearCzJgc();
        BigDecimal currYearCb =    jy.getBenYearCost();
        jy.setYearCzSubTotal(NumberUtil.adds(currYearCzYs,currYearCzSj,currYearCzJgc));
        BigDecimal currYearLr = NumberUtil.subtract(currYearCzYs, currYearCb);
        jy.setBenYearLr(currYearLr);
        jy.setBenYearLrl(NumberUtil.divideUseProportion(currYearLr,2,NumberUtil.add(currYearCzYs,currYearCzJgc)));

        // 累计工程利润
        BigDecimal nzYsHj = jy.getNodeBeforeYs();
        BigDecimal nzYsJgc = jy.getNodeBeforeYsJgc();
        BigDecimal nzCbHj = jy.getNodeBeforeCb();
        BigDecimal nzLr = NumberUtil.subtract(nzYsHj, nzCbHj);
        jy.setKlLr(nzLr);
        jy.setKlLrl(NumberUtil.divideUseProportion(nzLr,2,NumberUtil.add(nzYsHj,nzYsJgc)));

    }

    /**
     * 去年 预算
     * @param pmInfoIds
     * @return
     */
    public List<YearLjWccz> qnYs(List<String> pmInfoIds) {
        List<YearLjWccz> resList = new ArrayList<>();
        List<JtczResultBo> qnYsHj = jyczbbMapper.qnYsHj(pmInfoIds);
        Map<String, BigDecimal> qnYsHjMap = qnYsHj.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));
        List<JtczResultBo> qnYsSj = jyczbbMapper.qnYsSj(pmInfoIds);
        Map<String, BigDecimal> qnYsSjMap = qnYsSj.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));
        List<JtczResultBo> qnYsJgc = jyczbbMapper.qnYsJgc(pmInfoIds);
        Map<String, BigDecimal> qnYsJgcMap = qnYsJgc.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));
        for (String pc : pmInfoIds) {
            YearLjWccz wccz = new YearLjWccz();
            wccz.setPc(pc);
            wccz.setPdzhj(qnYsHjMap.getOrDefault(pc,BigDecimal.ZERO));
            wccz.setSj(qnYsSjMap.getOrDefault(pc,BigDecimal.ZERO));
            wccz.setJgc(qnYsJgcMap.getOrDefault(pc,BigDecimal.ZERO));
            resList.add(wccz);
        }
        return resList;
    }

    /**
     * 去年 成本
     * @param pmInfoIds
     * @return
     */
    public List<YearLjWccz> qnCb(List<String> pmInfoIds) {
        List<YearLjWccz> resList = new ArrayList<>();
        List<JtczResultBo> qnCbHj = jyczbbMapper.qnCbHj(pmInfoIds);
        Map<String, BigDecimal> qnCbHjMap = qnCbHj.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));
        List<JtczResultBo> qnCbJgc = jyczbbMapper.qnCbJgc(pmInfoIds);
        Map<String, BigDecimal> qnCbJgcMap = qnCbJgc.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));

        for (String pc : pmInfoIds) {
            YearLjWccz wccz = new YearLjWccz();
            wccz.setPc(pc);
            wccz.setPdzhj(qnCbHjMap.getOrDefault(pc,BigDecimal.ZERO));
            wccz.setJgc(qnCbJgcMap.getOrDefault(pc,BigDecimal.ZERO));
            resList.add(wccz);
        }
        return resList;
    }


    /**
     * 本年 产值
     * @param pmInfoIds
     * @return
     */
    public List<YearLjWccz> bnCz(List<String> pmInfoIds) {
        List<YearLjWccz> resList = new ArrayList<>();
        List<JtczResultBo> bnCzHj = jyczbbMapper.bnCzHj(pmInfoIds);
        Map<String, BigDecimal> bnCzHjMap = bnCzHj.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));
        List<JtczResultBo> bnCzSj = jyczbbMapper.bnCzSj(pmInfoIds);
        Map<String, BigDecimal> bnCzSjMap = bnCzSj.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));
        List<JtczResultBo> bnCzJgc = jyczbbMapper.bnCzJgc(pmInfoIds);
        Map<String, BigDecimal> bnCzJgcMap = bnCzJgc.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));
        List<JtczResultBo> bnCzQztj = jyczbbMapper.bnCzQztj(pmInfoIds);
        Map<String, BigDecimal> bnCzQztjMap = bnCzQztj.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));
        List<JtczResultBo> bnCzQzaz = jyczbbMapper.bnCzQzaz(pmInfoIds);
        Map<String, BigDecimal> bnCzQzazMap = bnCzQzaz.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));

        for (String pc : pmInfoIds) {
            YearLjWccz wccz = new YearLjWccz();
            wccz.setPc(pc);
            wccz.setPdzhj(bnCzHjMap.getOrDefault(pc,BigDecimal.ZERO));
            wccz.setSj(bnCzSjMap.getOrDefault(pc,BigDecimal.ZERO));
            wccz.setJgc(bnCzJgcMap.getOrDefault(pc,BigDecimal.ZERO));
            wccz.setTj(bnCzQztjMap.getOrDefault(pc,BigDecimal.ZERO));
            wccz.setAz(bnCzQzazMap.getOrDefault(pc,BigDecimal.ZERO));
            resList.add(wccz);
        }

        return resList;
    }


    /**
     * 本年 成本
     * @param pmInfoIds
     * @return
     */
    public List<YearLjWccz> bnCb(List<String> pmInfoIds) {
        List<YearLjWccz> resList = new ArrayList<>();
        List<JtczResultBo> bnCbHj = jyczbbMapper.bnCbHj(pmInfoIds);
        Map<String, BigDecimal> bnCbHjMap = bnCbHj.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));
        List<JtczResultBo> bnCbJgc = jyczbbMapper.bnCbJgc(pmInfoIds);
        Map<String, BigDecimal> bnCbJgcMap = bnCbJgc.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));
        for (String pc : pmInfoIds) {
            YearLjWccz wccz = new YearLjWccz();
            wccz.setPc(pc);
            wccz.setPdzhj(bnCbHjMap.getOrDefault(pc,BigDecimal.ZERO));
            wccz.setJgc(bnCbJgcMap.getOrDefault(pc,BigDecimal.ZERO));
            resList.add(wccz);
        }

        return resList;
    }

    /**
     * 年中  5/20 产值
     * @param pmInfoIds
     * @return
     */
    public List<YearLjWccz> nzCz(List<String> pmInfoIds) {
        List<YearLjWccz> resList = new ArrayList<>();
        List<JtczResultBo> nzCzHj = jyczbbMapper.nzCzHj(pmInfoIds);
        Map<String, BigDecimal> nzCzHjMap = nzCzHj.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));
        List<JtczResultBo> nzCzSj = jyczbbMapper.nzCzSj(pmInfoIds);
        Map<String, BigDecimal> nzCzSjMap = nzCzSj.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));
        List<JtczResultBo> nzCzJgc = jyczbbMapper.nzCzJgc(pmInfoIds);
        Map<String, BigDecimal> nzCzJgcMap = nzCzJgc.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));

        for (String pc : pmInfoIds) {
            YearLjWccz wccz = new YearLjWccz();
            wccz.setPc(pc);
            wccz.setPdzhj(nzCzHjMap.getOrDefault(pc,BigDecimal.ZERO));
            wccz.setSj(nzCzSjMap.getOrDefault(pc,BigDecimal.ZERO));
            wccz.setJgc(nzCzJgcMap.getOrDefault(pc,BigDecimal.ZERO));
            resList.add(wccz);
        }
        return resList;
    }


    /**
     * 年中  5/20 成本
     * @param pmInfoIds
     * @return
     */
    public List<YearLjWccz> nzCb(List<String> pmInfoIds) {
        List<YearLjWccz> resList = new ArrayList<>();

        List<JtczResultBo> nzCbHj = jyczbbMapper.nzCbHj(pmInfoIds);
        Map<String, BigDecimal> nzCbHjMap = nzCbHj.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));
        List<JtczResultBo> nzCbJgc = jyczbbMapper.nzCbJgc(pmInfoIds);
        Map<String, BigDecimal> nzCbJgcMap = nzCbJgc.stream().collect(Collectors.toMap(JtczResultBo::getText, JtczResultBo::getVal, (v1, v2) -> v2));
        for (String pc : pmInfoIds) {
            YearLjWccz wccz = new YearLjWccz();
            wccz.setPc(pc);
            wccz.setPdzhj(nzCbHjMap.getOrDefault(pc,BigDecimal.ZERO));
            wccz.setJgc(nzCbJgcMap.getOrDefault(pc,BigDecimal.ZERO));
            resList.add(wccz);
        }
        return resList;
    }


}
